/**
 * 
 */
package com.easidea.tim.action;

import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import com.easidea.tim.action.base.BaseAction;
import com.easidea.tim.service.system.orgnization.StaffService;
import com.opensymphony.webwork.ServletActionContext;

/**
 * 默认Action
 * @author Harrison
 *
 */
public class IndexAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8219312383160701339L;
	
	private StaffService staffService;

	private String username;
	private String password;
	/**
	 * login action the first action for this application
	 * @return
	 * @throws Exception
	 */
	public String loginPage() throws Exception {
		Map<Object, Object> session = getSession();
		if(session.containsKey(ActionConstants.LOGGED_USER))
			return "index";
		return "LogonPage";
	}
	
	public String switchLogin(){
		if(username.equals("admin")&&password.equals("admin")){
			return "login";
		}else{
			return "error";
		}
	}
	/**
	 * 处理用户登录，将返回信息直接通过response.writeln的方式返回
	 * @return
	 */
	public String doLogin() throws Exception{
		if(username != null && password != null){
			int rst = staffService.staffLogin(username, password);
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setCharacterEncoding("UTF-8");
			PrintWriter writer = response.getWriter();
			switch (rst){
				case -1:
					writer.write("{success: false, error: '用户名不存在！'}");
					break;
				case -2:
					writer.write("{success: false, error: '口令错误！'}");
					break;
				case -3:
					writer.write("{success: false, error: '用户状态错误，请联系管理员！'}");
					break;
				case 0:
					writer.write("{success: false, error: '系统错误，请联系管理员！'}");
					break;
				case 1:
					writer.write("{success: true}");
					break;
			}
		}
		return null;
	}

	/**
	 * @return the username
	 */
	public String getUsername() {
		return username;
	}

	/**
	 * @param username the username to set
	 */
	public void setUsername(String username) {
		this.username = username;
	}

	/**
	 * @return the password
	 */
	public String getPassword() {
		return password;
	}

	/**
	 * @param password the password to set
	 */
	public void setPassword(String password) {
		this.password = password;
	}

	/**
	 * @return the staffService
	 */
	public StaffService getStaffService() {
		return staffService;
	}

	/**
	 * @param staffService the staffService to set
	 */
	public void setStaffService(StaffService staffService) {
		this.staffService = staffService;
	}
}
